package org.dioib.practice.common.exception.handler;

import org.dioib.practice.common.ApiResponse;
import org.dioib.practice.common.exception.InventoryShortageException;
import org.dioib.practice.common.exception.OrderNotFoundException;
import org.dioib.practice.common.exception.OrderStatusException;
import org.dioib.practice.common.exception.PaymentFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestControllerAdvice(basePackages = "com.example.order.controller")
public class OrderExceptionHandler {


    @ExceptionHandler(OrderNotFoundException.class)
    public ResponseEntity<ApiResponse<?>> handleOrderNotFound(
            OrderNotFoundException ex, WebRequest request) {

        Map<String, Object> details = new HashMap<>();
        details.put("orderId", ex.getOrderId());
        details.put("suggestions", List.of("检查订单ID是否正确", "联系客服查询订单状态"));


        ApiResponse<?> response = ApiResponse.error(
                        HttpStatus.NOT_FOUND.value(),
                        "ORDER_NOT_FOUND",
                        ex.getMessage(),
                        details
                );
        response.setTimestamp(LocalDateTime.now());
        response.setPath(request.getDescription(false));


        return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
    }


    @ExceptionHandler(InventoryShortageException.class)
    public ResponseEntity<ApiResponse<?>> handleInventoryShortage(
            InventoryShortageException ex, WebRequest request) {

        Map<String, Object> details = new HashMap<>();
        details.put("productId", ex.getProductId());
        details.put("requestedQuantity", ex.getRequestedQuantity());
        details.put("availableQuantity", ex.getAvailableQuantity());


        ApiResponse<?> response = ApiResponse.error(
                        HttpStatus.CONFLICT.value(),
                        "INSUFFICIENT_INVENTORY",
                        ex.getMessage(),
                        details
                );
        response.setTimestamp(LocalDateTime.now());
        response.setPath(request.getDescription(false));


        return new ResponseEntity<>(response, HttpStatus.CONFLICT);
    }
    @ExceptionHandler(PaymentFailedException.class)
    public ResponseEntity<ApiResponse<?>> handlePaymentFailed(
            PaymentFailedException ex, WebRequest request) {

        Map<String, Object> details = new HashMap<>();
        details.put("paymentMethod", ex.getPaymentMethod());
        details.put("failureReason", ex.getFailureReason());
        details.put("suggestions", List.of("检查支付信息", "尝试其他支付方式", "联系银行查询"));


        ApiResponse<?> response = ApiResponse.error(
                        HttpStatus.PAYMENT_REQUIRED.value(),
                        "PAYMENT_FAILED",
                        ex.getMessage(),
                        details
                );
        response.setTimestamp(LocalDateTime.now());
        response.setPath(request.getDescription(false));


        return new ResponseEntity<>(response, HttpStatus.PAYMENT_REQUIRED);
    }


    @ExceptionHandler(OrderStatusException.class)
    public ResponseEntity<ApiResponse<?>> handleOrderStatus(
            OrderStatusException ex, WebRequest request) {

        Map<String, Object> details = new HashMap<>();
        details.put("currentStatus", ex.getCurrentStatus());
        details.put("requiredStatus", ex.getAllowedStatuses());

        ApiResponse<?> response = ApiResponse.error(
                        HttpStatus.CONFLICT.value(),
                        "INVALID_ORDER_STATUS",
                        ex.getMessage(),
                        details
                );

        response.setTimestamp(LocalDateTime.now());
        response.setPath(request.getDescription(false));


        return new ResponseEntity<>(response, HttpStatus.CONFLICT);
    }
}